/**
 * Created by leo on 1/18.
 */
var gulp = require('gulp');
var concat = require('gulp-concat');
var uglify = require('gulp-uglify');
var ngAnnotate = require('gulp-ng-annotate');
var rename = require('gulp-rename');
var del = require('del');
var ngFilesort = require('gulp-angular-filesort');
var ngTemplatecache = require('gulp-angular-templatecache');
var htmlMin = require('gulp-htmlmin');
var htmlMinOptions = {
    minifyJS: true,
    minifyCSS: true,
    keepClosingSlash: true,
    collapseBooleanAttributes: true,
    collapseWhitespace: true,
    conservativeCollapse: true,
    removeAttributeQuotes: true,
    removeComments: true,
    removeEmptyAttributes: true,
    removeRedundantAttributes: true,
    removeScriptTypeAttributes: true,
    removeStyleLinkTypeAttributes: true
};
var concatCss = require('gulp-concat-css');
var minifyCSS = require('gulp-minify-css');
var inject = require('gulp-inject');
var injectString = require('gulp-inject-string');
var es = require('event-stream');
var webserver = require('gulp-webserver');
var img64 = require('gulp-img64');
var staticHash = require('gulp-static-hash');
var sort = require('gulp-sort');

gulp.task('bundle', [], function () {
    gulp.src(['portal-api.js', 'portal-embled.js']).pipe(uglify()).pipe(rename({suffix: '.min'})).pipe(gulp.dest('./'));
    var jsStream = gulp.src(['app/**/*.js', '!app/*-dev.js'], {base: 'app'})
            .pipe(ngAnnotate()),
        templatesStream = gulp.src('app/**/*.html')
            .pipe(sort())
            .pipe(htmlMin(htmlMinOptions))
            .pipe(ngTemplatecache({root: 'app/'}));

    return es.merge(jsStream, templatesStream)
        .pipe(sort())
        .pipe(ngFilesort())
        .pipe(concat('bundle.js'))
        //.pipe(gulp.dest('dist'))
        //.pipe(uglify())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist'));
});

gulp.task('vendor', [], function () {
    return gulp.src([
        'libs/jquery/1.11.1/jquery.min.js',
        'libs/angular-1.2.x/angular.min.js',
        'libs/angular-1.2.x/angular-animate.js',
        'libs/highcharts-4.0.4/highcharts.js',
        'libs/moment/moment.min.js',
        'libs/js-yaml/js-yaml.min.js',
        'libs/jquery-easyui-1.3.3/jquery.easyui.min.js',
        'libs/slick-1.3.15/slick.min.js'], {base: './'})
        .pipe(concat('vendor.js'))
        //.pipe(gulp.dest('dist'))
        //.pipe(uglify())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist'));
});

gulp.task('scripts', ['vendor', 'bundle', 'css']);

gulp.task('jsp', ['scripts', 'bigscreen'], function () {
    return gulp.src('index.html')
        .pipe(inject(gulp.src('dist/bundle.min.css', {read: false}), {relative: true}))
        .pipe(inject(gulp.src('dist/vendor.min.js', {read: false}), {name: 'vendor', relative: true}))
        .pipe(inject(gulp.src('dist/bundle.min.js', {read: false}), {name: 'bundle', relative: true}))
        .pipe(img64())
        .pipe(staticHash({asset: 'dist', exts: ['js', 'css']}))
        // <!-- 引入服务管理全局遮罩层需要的js -->
        .pipe(injectString.before('</body>',
            '<link href="../nresources/basd/thickbox/thickbox.css" rel="stylesheet"/>\
            <script src="../njs/basd/thickbox/thickbox.js"></script>'))
        .pipe(htmlMin(htmlMinOptions))
        .pipe(rename('index.jsp'))
        .pipe(gulp.dest(''))
        .pipe(injectString.before('</body>',
            '<script src="libs/angular-1.2.x/angular-mocks.js"></script><script src="app/index-dev.js"></script>'))
        .pipe(rename('index-min.html'))
        .pipe(gulp.dest(''));
});

gulp.task('bigscreen', ['scripts'], function () {
    return gulp.src('bigscreen.html')
        .pipe(inject(gulp.src('dist/bundle.min.css', {read: false}), {relative: true}))
        .pipe(inject(gulp.src('dist/vendor.min.js', {read: false}), {name: 'vendor', relative: true}))
        .pipe(inject(gulp.src('dist/bundle.min.js', {read: false}), {name: 'bundle', relative: true}))
        .pipe(img64())
        .pipe(staticHash({asset: 'dist', exts: ['js', 'css']}))
        .pipe(htmlMin(htmlMinOptions))
        .pipe(rename('bigscreen.jsp'))
        .pipe(gulp.dest(''))
        .pipe(injectString.before('</body>',
            '<script src="libs/angular-1.2.x/angular-mocks.js"></script><script src="app/index-dev.js"></script>'))
        .pipe(rename('bigscreen-min.html'))
        .pipe(gulp.dest(''));
});

gulp.task('css', function () {
    return gulp.src(['libs/pure/pure-min.css',
        'libs/font-awesome-4.2.0/css/font-awesome.min.css',
        'libs/slick-1.3.15/slick.css',
        'libs/jquery-easyui-1.3.3/themes/default/easyui.css',
        'libs/jquery-easyui-1.3.3/themes/icon.css',
        'styles/style.css',
        'app/**/*.css'], {base: './'})
        .pipe(sort())
        .pipe(concatCss('dist/bundle.css'))
        //.pipe(gulp.dest(''))
        .pipe(minifyCSS())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest(''));
});

gulp.task('clean', function (cb) {
    return del(['dist', '*.jsp', '*-min.html', '!panel-conf-cache.jsp'], cb);
});

gulp.task('watch', function () {
    gulp.watch(['app/**/*.js', 'app/**/*.html'], ['scripts', 'jsp']);
    gulp.watch(['styles/**/*.css', 'app/**/*.css'], ['css', 'jsp']);
});

gulp.task('webserver', function () {
    gulp.src('./')
        .pipe(webserver({host: '0.0.0.0'}));
});

gulp.task('default', ['clean', 'jsp']);